package chap10collection.test17;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author zdl
 * @date 2024/12/17
 */
class Worker{
    private String name;
    private Integer age;
    private Double salary;
    private Address address;


    public Worker() {
    }

    public Worker(String name, Integer age, Double salary, Address address) {
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.address = address;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Worker worker = (Worker) o;
        return Objects.equals(name, worker.name) && Objects.equals(age, worker.age) && Objects.equals(salary, worker.salary) && Objects.equals(address, worker.address);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary, address);
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public Integer getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(Integer age) {
        this.age = age;
    }

    /**
     * 获取
     * @return salary
     */
    public Double getSalary() {
        return salary;
    }

    /**
     * 设置
     * @param salary
     */
    public void setSalary(Double salary) {
        this.salary = salary;
    }

    /**
     * 获取
     * @return address
     */
    public Address getAddress() {
        return address;
    }

    /**
     * 设置
     * @param address
     */
    public void setAddress(Address address) {
        this.address = address;
    }

    public String toString() {
        return "Worker{name = " + name + ", age = " + age + ", salary = " + salary + ", address = " + address + "}";
    }
}
class Address{
    private String addressName;
    private String zipCode;

    public Address() {
    }

    public Address(String addressName, String zipCode) {
        this.addressName = addressName;
        this.zipCode = zipCode;
    }



    /**
     * 获取
     * @return addressName
     */
    public String getAddressName() {
        return addressName;
    }

    /**
     * 设置
     * @param addressName
     */
    public void setAddressName(String addressName) {
        this.addressName = addressName;
    }

    /**
     * 获取
     * @return zipCode
     */
    public String getZipCode() {
        return zipCode;
    }

    /**
     * 设置
     * @param zipCode
     */
    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    public String toString() {
        return "Address{addressName = " + addressName + ", zipCode = " + zipCode + "}";
    }
}
public class Demo17 {
    public static void main(String[] args) {
        /**
         * 要求，完善 Worker 和 Address 类，使得 Worker 对象能够正确放入 HashSet
         * 中：即将 Worker 放入 HashSet 中时不会出现重复元素。并编写相应测试代码。
         * 思路分析：使用HashSet存入不重复对象  需要重写equals 和 hashCode方法
         */
        Address address1 = new Address("郑州","10081");
        Worker w1 = new Worker("张三",20,2000.0,address1);
        Worker w2 = new Worker("张三",20,2000.0,address1);
        Worker w3 = new Worker("张三三",20,2000.0,address1);
        Set<Worker> set = new HashSet<>();
        set.add(w1);
        set.add(w2);
        set.add(w3);
        System.out.println(set);
        System.out.println(set.size());
    }

}
